<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	private $_user;
	
	public function authenticate()
	{
		$loginUser = TbUser::model()->findByAttributes(array('username'=>$this->username));

		if($loginUser === null){
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		}else{
		
		 if(!Encrypt::Decript($this->password, $loginUser->salt, $loginUser->password)){
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
				//print_r($this->errorCode);
				//die($this->errorCode);
		 }
		else
		{
			$this->errorCode=self::ERROR_NONE;
			//lưu lại thông tin user đang đăng nhập
			$this->_user = $loginUser;
		}
		//print_r($this->_user);
		//die();
		}
		return !$this->errorCode;
	}
	
	public function getId(){
		return $this->_user;
	}
}